/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sss.services;

import java.util.ArrayList;
import java.util.List;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import org.sss.tools.Product;
import org.sss.tools.ProductShop;
import org.sss.tools.Shop;
import org.sss.tools.SssHelper;

/**
 * Class WebService. 
 * Class is responsible for receiving and sending queries to/from client applications.
 * @author Przemyslaw Wojtysiak
 */
@WebService(serviceName = "SupportShoppingSystemService")
public class SupportShoppingSystemService {
    
    /**
     * The method returns a list ProductShop for the shop and for a given product name. 
     * In the case of idShop = -1 returns a list of ProductShop for all stores.
     * In the case of the empty name of the product, return all products for a given store.
     * @param name
     * @param idShop
     * @return list of ProductShop
     */
    @WebMethod(operationName = "getProductShopByNameProduct")
    public List<ProductShop> getProductShopByNameProduct(@WebParam(name="name") String name,
                                                        @WebParam(name="idShop") int idShop) {
        SssHelper sssHelper = new SssHelper();
        List productShops = null;
        
        if(name.equals("")){
            productShops = sssHelper.getProductShopByIdShop(idShop);
        }
        else {
            productShops= sssHelper.getProductShopByNameProduct(name, idShop);
        }
        return productShops;
    }
    
    /**
     * The method returns a list of the Product for any given store.
     * @param shop
     * @return list of Product
     */
    @WebMethod(operationName="getProductsByShop")
    public List<Product> getProductsByShop(@WebParam(name="shop") Shop shop) {
        SssHelper sssHelper = new SssHelper();
        List products = sssHelper.getProductsByShop(shop);
        return products;
    }
    
    /**
     * The method of verifying the user's permissions (now only for normal user and new user). 
     * For future  for admin, owner shop and blocked user.
     * If the user has logged on for the first time the application 
     * is automatically added to the database with the status of "normal user"
     * @param fb_uid
     * @return
     * 0 - blocked user
     * 1 - normal user
     * 2 - admin
     * 3 - owner shop
     * 4 - new user (normal user)
     */
    @WebMethod(operationName="checkUser")
    public int checkUser(@WebParam(name="fb_uid") long fb_uid) {
        SssHelper sssHelper = new SssHelper();
        int result = sssHelper.checkUser(fb_uid);
        return result;
    }
    
    /**
     * The method returns a list of Shop for shops located within 200m from the given coordinates.
     * @param x
     * @param y
     * @param city
     * @return
     */
    @WebMethod(operationName="getShopByCoordinates")
    public List<Shop> getShopByCoordinates(@WebParam(name="x") Double x,
                                @WebParam(name="y") Double y,
                                @WebParam(name="city") String city) {
        SssHelper sssHelper = new SssHelper();
        List<Shop> result = sssHelper.getShopByCoordinates(x, y, city);
        return result;
    }
    
    /**
     * The method returns a list of product names for a given user.
     * @param fb_uid
     * @return list of String
     */
    @WebMethod(operationName="checkSavedShoppingList")
    public List<String> checkSavedShoppingList(@WebParam(name="fb_uid") long fb_uid) {
        SssHelper sssHelper = new SssHelper();
        List result = sssHelper.checkSavedShoppingList(fb_uid);
        return result;
    }

    /**
     * Method of Writing a new shopping list for that user.
     * @param fb_uid
     * @param shoppingList
     * @return
     */
    @WebMethod(operationName="saveShoppingList")
    public int saveShoppingList(@WebParam(name="fb_uid") long fb_uid,
                                @WebParam(name="shoppingList") String shoppingList) {
        SssHelper sssHelper = new SssHelper();
        int result = sssHelper.saveShoppingList(fb_uid,shoppingList);
        return 1;
    }
    
    /**
     * The method for starting the main algorithm to search for stores with products with an optimal price.
     * For Android application.
     * @param params
     * @param products
     * @return list of ProductShop
     */
    @WebMethod(operationName="searchProductAndroid")
    public List<ProductShop> searchProductAndroid(@WebParam(name="params") String params,
                                            @WebParam(name="products") String products) {
        SssHelper sssHelper = new SssHelper();
        String[] tmp = products.split(";");
        List<String> tmpProduct = new ArrayList<String>();
        for (int i = 0; i < tmp.length; i++) {
            tmpProduct.add(tmp[i]);
            System.out.println(tmp[i]);
        }
        String[] tmp2 = params.split(";");
        Double x = Double.parseDouble(tmp2[0]);
        Double y = Double.parseDouble(tmp2[1]);
        String city = tmp2[2];
        String country = tmp2[3];
        int transport = Integer.parseInt(tmp2[4]);
        int time = Integer.parseInt(tmp2[5]);
        
        List searchedProducts = sssHelper.searchProduct(x, y, tmpProduct, city, country, transport, time);
        
        
        return searchedProducts;
        
        
    }
    
    /**
     * The method for starting the main algorithm to search for stores with products with an optimal price.
     * @param x
     * @param y
     * @param products
     * @param city
     * @param country
     * @param transport
     * @param time
     * @return list of ProductShop
     */
    @WebMethod(operationName="searchProduct")
    public List<ProductShop> searchProduct(@WebParam(name="x") Double x,
                     @WebParam(name="y") Double y,
                     @WebParam(name="products") List<String> products,
                     @WebParam(name="city") String city,
                     @WebParam(name="country") String country, 
                     @WebParam(name="transport") int transport,
                     @WebParam(name="time") int time) {
        SssHelper sssHelper = new SssHelper();
        
        List searchedProducts = sssHelper.searchProduct(x, y, products, city, country, transport, time);
        
        
        return searchedProducts;
        
        
    }
    
    /**
     * The method responsible for adding / subtracting points for the product.
     * @param idProduct
     * @param fb_uid
     * @param value
     * @return
     * 0 - product was plused before
     * 1 - plus product correctly
     * 2 - some error - if product didn't exist in database
     */
    @WebMethod(operationName="plusProduct")
    public int plusProduct(@WebParam(name="idProduct") int idProduct,
                    @WebParam(name="fb_uid") long fb_uid,
                    @WebParam(name="value") int value) {
        SssHelper sssHelper = new SssHelper();
        int result = sssHelper.plusProduct(idProduct,fb_uid, value);
        return result;
    }
    
    /**
     * The method returns a list containing all the stores in the database.
     * @return list of Shop
     */
    @WebMethod(operationName = "getShops")
    public List<Shop> getShops() {
        SssHelper sssHelper = new SssHelper();
        List shops = sssHelper.getShops();
        return shops;
    }
    
    /**
     * The method returns a list containing all ProductShop in the database.
     * @return
     */
    @WebMethod(operationName = "getProductShop")
    public List<ProductShop> getProductShop() {
        SssHelper sssHelper = new SssHelper();
        List productShops = sssHelper.getProductShop();
        return productShops;
    }
    
    /**
     * The method returns a list containing the given number of ProductShop in the database.
     * @param numberOfProductShops
     * @return list of ProductShop
     */
    @WebMethod(operationName = "getRightAmountProductShop")
    public List<ProductShop> getRightAmountProductShop(@WebParam(name="amount") int amount) {
        SssHelper sssHelper = new SssHelper();
        List productShops = sssHelper.getRightAmountProductShop(amount);
        return productShops;
    }
    
    /**
     * Method adds a new product
     * @param fb_uid
     * @param product
     * @param shop
     * @return
     * 0 - existed in database
     * 1 - added corectly
     */
    @WebMethod(operationName="addNewProduct")
    public int addNewProduct(@WebParam(name = "fb_uid") long fb_uid, 
                                @WebParam(name = "product") Product product,
                                @WebParam(name = "shop") Shop shop) {
        SssHelper sssHelper = new SssHelper();
        int result = sssHelper.addNewProduct(fb_uid, product, shop);
        return result;
    }
    
    
    /**
     * The method returns the names of all the shops in the city.
     * @param name of city
     * @return list of String
     */
    @WebMethod(operationName="getShopName")
    public List<String> getShopName(@WebParam(name = "name") String name) {
        SssHelper sssHelper = new SssHelper();
        List shopName = sssHelper.getShopName(name);
        return shopName;
    }
    
    /**
     * The method returns a list of Shop store for a given name.
     * @param name
     * @return list of Shop
     */
    @WebMethod(operationName="getShopByName")
    public List<Shop> getShopByName(@WebParam(name="name") String name) {
        SssHelper sssHelper = new SssHelper();
        List shopByName = sssHelper.getShopByName(name);
        return shopByName;
    }
    
    /**
     * The method returns list of Shop of all the shops in the city.
     * @param name
     * @return list of Shop
     */
    @WebMethod(operationName="getShopByCity")
    public List<Shop> getShopByCity(@WebParam(name="name") String name) {
        SssHelper sssHelper = new SssHelper();
        List shopByName = sssHelper.getShopByCity(name);
        return shopByName;
    }
    
    /**
     * The method returns a list of Product of the given name of the product.
     * @param name
     * @return list of Product
     */
    @WebMethod(operationName="getProductByName")
    public List<Product> getProductByName(@WebParam(name="name") String name) {
        SssHelper sssHelper = new SssHelper();
        List ProductByName = sssHelper.getProductByName(name);
        return ProductByName;
    }
    

    /**
     * This method adds a new store. / / Method for the Administrator.
     * @param name
     * @param street
     * @param number
     * @param city
     * @param country
     * @param x
     * @param y
     */
    @WebMethod(operationName="addNewShop")
    public void addNewShop(@WebParam(name="name") String name,
                            @WebParam(name="street") String street,
                            @WebParam(name="number") int number,
                            @WebParam(name="city") String city,
                            @WebParam(name="country") String country,
                            @WebParam(name="x") Double x,
                            @WebParam(name="y") Double y) {
        SssHelper sssHelper = new SssHelper();
        Shop shop = new Shop(name, street, number, city, country, x, y);
        sssHelper.addNewShop(shop);
    }
    
    
    /**
     * The method of editing the existing product. // For future functionality.
     * @param idProduct
     * @param newProduct
     */
    @WebMethod(operationName="editProduct")
    public void editProduct(@WebParam(name="idProduct") int idProduct,
                            @WebParam(name="newProduct") Product newProduct)
    {
        SssHelper sssHelper = new SssHelper();
        sssHelper.editProduct(idProduct,newProduct);
    }
}
